//Project 3 Group 10 - MainActivity

package com.example.p4;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import com.playtomic.android.api.*;

//MainActivity is the main menu of our app
//It allows the user to play the game or view highscores

public class MainActivity extends Activity {
	
	double score = 0;
	int lives = 0;
	ArrayList<Class> gameOrder;
	int gameIndex = 0;
	long gameStartTime;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Typeface font = Typeface.createFromAsset(getAssets(), "agentorange.ttf");
        
        //Setup buttons
        Button playButton = (Button) findViewById(R.id.play_button);
        Button highScoreButton = (Button) findViewById(R.id.high_score_button);
        
        playButton.setTextSize(28);
        playButton.setTextColor(Color.WHITE);
        playButton.setTypeface(font);
        highScoreButton.setTextSize(28);
        highScoreButton.setTextColor(Color.WHITE);
        highScoreButton.setTypeface(font);
        
        //Set up highscores through Playtomic.com
        setUpHighScores();
        
        playButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	startGame();
            }
        });
        
        highScoreButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                launchHighscores();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        Log.d("onActivityResult", "requestCode: "+requestCode +", resultCode: "+resultCode);

        if (requestCode == 0) {
            if (resultCode == Activity.RESULT_OK) {
            	score = data.getDoubleExtra("score", 0); 
            	Log.d("score", "Received score of "+score);
            	if(score == 0)
            		lives--;
            }
        }
        Log.d("GameEvent", "Number of Lives: "+lives);
        if(lives > 0)
        	launchNextGame();
        else
        {
        	int gamePlayTime = (int)((System.currentTimeMillis() - gameStartTime)/1000);
        	Log.d("score", "Time played = "+gamePlayTime);
        	Intent i = new Intent(getApplicationContext(), GameFinish.class);
        	i.putExtra("score", gamePlayTime);//Convert to seconds
        	startActivity(i);
        }
   }
    
    private void startGame()
    {
    	lives = 3;
    	
    	gameOrder = new ArrayList<Class>();
    	gameOrder.add(Maze.class);
    	gameOrder.add(FallingBalls.class);
    	gameOrder.add(PopBubble.class);
    	
    	for(int i=0; i<10; i++)
    	{
    		int pos1 = (int)(Math.random()*gameOrder.size());
    		int pos2 = (int)(Math.random()*gameOrder.size());
    		
    		Class<?> pos1Class =   gameOrder.get(pos1);
    		Class<?> pos2Class =   gameOrder.get(pos2);
    		gameOrder.set(pos1, pos2Class);
    		gameOrder.set(pos2, pos1Class);
    	}
    	
    	gameStartTime = System.currentTimeMillis();
    	launchNextGame();
    }
    
    private void launchNextGame()
    {
    	launchActivity( gameOrder.get(gameIndex%gameOrder.size()) );
    	gameIndex++;
    }
    
    private void setUpHighScores()
    {
    	//Connect to Playtomic.com and initialize their API with our Game's data
    	try {
		    Playtomic.getInstance(
		    		951371, 
		    		"bd563d06fbf943ed", 
		    		"7ade247eff2549eb91f4c3b9d97d9b", 
		    		this);
		    Playtomic.Log().view();
			
			Log.i("Highscores", "Set up the Playtomic (highscores) connection");
		}
		catch (Exception ex) {
			Log.e("Highscores", "Failed to set up Playtomic (highscores) connection");
		}
    }
    
    protected void launchActivity(Class<?> launchingClass)
    {
    	Intent i = new Intent(this, launchingClass);
    	i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivityForResult(i, 0);
    }
    
    protected void launchHighscores() {
        Intent i = new Intent(this, ViewHighscoreActivity.class);
        startActivity(i);
    }
    
}

